package BNS.kingPawn;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Hashtable;
import BNS.graphs.*;

/** The controller piece of a Model/View/Control architecture for the
	player's GUI in MarketISPersona. 
	*/
public class ControlPanel extends JPanel
                        implements ActionListener, ChangeListener {
	
	private JLabel kingRate, pawnRate;
	private JButton kingButton, pawnButton;
	JSlider tippingRate;
	private MarketISPersona parent;
	private int oldTipPercent= 0;
	
	/** Creates all the GUI elements in the bottom ("control") panel of the Persona.
		@param p the MarketISPersona that contains this subpanel; will be used to announce user actions
		@param tippy boolean flag indicating that tips are allowed in this game.
		*/
	public ControlPanel(MarketISPersona p, boolean tippy) { 
		parent=p;
		GridBagLayout gridbag= new GridBagLayout();
		GridBagConstraints constraints= new GridBagConstraints();
        setLayout(gridbag);
		
		constraints.fill= GridBagConstraints.BOTH;
		constraints.weightx= 1.0;
		
		if (tippy) {//Create the tipping widgets.
        JLabel sliderLabel= new JLabel("King's total tip rate: ", JLabel.RIGHT);
		gridbag.setConstraints(sliderLabel, constraints);
		add(sliderLabel);
		
        tippingRate= new JSlider(JSlider.HORIZONTAL,0,10,0);
		tippingRate.setSnapToTicks(true);
		Hashtable tipMarks= new Hashtable();
		for (int i=0; i<=10; i+=2) 
			tipMarks.put(new Integer(i), new JLabel((10*i)+"%"));
        tippingRate.setLabelTable(tipMarks);
		tippingRate.addChangeListener(this);
        tippingRate.setMajorTickSpacing(2);
        tippingRate.setMinorTickSpacing(1);
        tippingRate.setPaintTicks(true);
        tippingRate.setPaintLabels(true);
		
//		System.err.println("size "+ tippingRate.getMaximumSize());
		tippingRate.setMaximumSize(new Dimension(400,100));
//		System.err.println("size "+ tippingRate.getMaximumSize());
//		System.err.println("size "+ tippingRate.getMinimumSize());
		tippingRate.setMinimumSize(new Dimension(400,100));
//		System.err.println("size "+ tippingRate.getMinimumSize());
		tippingRate.setBackground(MarketISPersona.panelBackground);
    
		tippingRate.setAlignmentX(Component.CENTER_ALIGNMENT);
		constraints.gridwidth= GridBagConstraints.REMAINDER; //end row
		gridbag.setConstraints(tippingRate, constraints);
		add(tippingRate);
		}
		
		//Create the coloring widgets.
		JLabel role= new JLabel("your role:",JLabel.RIGHT);
		constraints.gridwidth= GridBagConstraints.RELATIVE;
		gridbag.setConstraints(role, constraints);
		add(role);
		
		JPanel colorPanel= new JPanel(new FlowLayout());
		colorPanel.setBackground(MarketISPersona.panelBackground);
		kingButton= new JButton("King");
		kingButton.setBackground(GraphPanel.swatches[MarketISGame.kingInt].color);
		kingButton.addActionListener(this);
		colorPanel.add(kingButton);
		kingRate= new JLabel("-?-",JLabel.LEFT);
		pawnRate= new JLabel("-?-",JLabel.LEFT);
		System.err.println("gotta set tipFrac to "+ 0.0 +" for "+kingRate);
		colorPanel.add(kingRate);
		
		pawnButton= new JButton("Pawn");
		pawnButton.setBackground(GraphPanel.swatches[MarketISGame.pawnInt].color);
		pawnButton.addActionListener(this);
		colorPanel.add(pawnButton);
		colorPanel.add(pawnRate);
		
		colorPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		constraints.gridwidth= GridBagConstraints.REMAINDER; //end row
		gridbag.setConstraints(colorPanel, constraints);
		add(colorPanel);
		
		setOpaque(true);
		setBackground(MarketISPersona.panelBackground);
		}
	
    /** Listen to the slider, and call setTipRate() when appropriate.
    	Required by ChangeListener interface. 
    	*/
    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
        if (!source.getValueIsAdjusting()) {
			int tipPercent = (int)source.getValue();
			System.err.println("gonna set tipFrac, tipNotch= "+ tipPercent);
			if (parent==null) setTipRate(tipPercent/10.0);
			else 
				if (!parent.setTipRate(tipPercent/10.0))
				tippingRate.setValue(oldTipPercent);
				else oldTipPercent= tipPercent;
			}
		}
	
	/** Redisplay kingRate and pawnRate values */
	protected void update(double tFrac, double kRate, double pRate) {
		//System.err.println("gotta set tipFrac to "+ tFrac +" for "+kingRate);
//		int tipNotch= (int)((tFrac*100. + .01)/10);
//		tippingRate.setValue(tipNotch);
		String rate= String.format("$%.2f/minute       ", kRate);
		kingRate.setText(rate);
		rate= String.format("$%.2f/minute ", pRate);
		pawnRate.setText(rate);
		}
	
	/** Handles actions caused by kingButton and pawnButton.
		Required by ActionListener interface. */
	public void actionPerformed(ActionEvent e) {
		Object src= e.getSource();
		if (parent==null) { System.err.println(" actionPerformed "+src); return; }
		if (src==kingButton) parent.setColor(2);
		if (src==pawnButton) parent.setColor(1);
		}
	
	private void setColor(int n) { System.err.println("remove this method!"); }	// TODO
	private void setTipRate(double newTip) {	// goofy. Just for testing by main.
		System.err.println(" tip changed to "+newTip); 
		}
	
	
    /**
     Create the GUI and show it.
	 For thread safety, this method should be invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("ControlPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
        //Create and set up the content pane.
        ControlPanel animator = new ControlPanel(null,true);
        animator.setOpaque(true); //content panes must be opaque
        frame.setContentPane(animator);
		
        //Display the window.
        frame.pack();
        frame.setVisible(true);
		
		System.err.println("size "+ animator.tippingRate.getSize());
		animator.tippingRate.setSize(new Dimension(400,100));
		System.err.println("size "+ animator.tippingRate.getSize());
		}
	
	/** just for testing during development */
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() { createAndShowGUI(); }
			});
		}
	}
